'use strict';

module.exports = app => {
    const { INTEGER, STRING, DATE, BIGINT, BOOLEAN } = app.Sequelize;

    const Theme = app.model.define('theme', {
        id: { type: BIGINT, primaryKey: true, autoIncrement: true },
        user_id: BIGINT,
        category_id: BIGINT,
        anonymity: BOOLEAN,
        content: STRING(1000),
        reading: INTEGER,
        online: BOOLEAN,
        recommend: BOOLEAN,
        create_time: DATE,
        update_time: DATE,
        delete_time: DATE
    }, {
        timestamps: true,
        paranoid: true,
        createdAt: 'create_time',
        updatedAt: 'update_time',
        deletedAt: 'delete_time'
    });

    Theme.associate = () => {
        app.model.Theme.hasMany(app.model.ThemeImage, {
            as: 'images',
            foreignKey: 'theme_id',
            targetKey: 'id',
        });

        app.model.Theme.belongsTo(app.model.ThemeCategory, {
            as: 'category',
            foreignKey: 'category_id',
            targetKey: 'id'
        });

        app.model.Theme.belongsTo(app.model.User, {
            as: 'user',
            foreignKey: 'user_id',
            targetKey: 'id'
        });

        app.model.Theme.hasMany(app.model.ThemeComment, {
            as: 'comment',
            foreignKey: 'theme_id',
            targetKey: 'id',
        });

        app.model.Theme.hasMany(app.model.ThemeLike, {
            as: 'like',
            foreignKey: 'theme_id',
            targetKey: 'id',
        });

    };


    return Theme;
}